package tw.edu.tku.im.maze;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.os.Parcel;
import android.os.Parcelable;

public class RoomListRep extends RepPacket {
   
   Room[] list;

   //-------------------------------------------------------
   public RoomListRep() {
      this.list = null;
   }
   
   public RoomListRep(Room[] list) {
      this.list = list;
   }
   
   public RoomListRep(List<Room> list) {
      this.list = new Room[list.size()];
      list.toArray(this.list);
   }
   
   //-------------------------------------------------------
   public Room[] getList() {
      return list;
   }

   public void setList(Room[] list) {
      this.list = list;
   }
   
   //-------------------------------------------------------
   public String toJSONString() throws JSONException {
      JSONObject jsonObj = new JSONObject();
      if(list!=null) {
         JSONArray roomList = new JSONArray();
         for(Room room : list) {
            if(room==null){ break; }
            roomList.put(new JSONObject(room.toJSONString()));
         }
         jsonObj.put("result", roomList);
         jsonObj.put("action", "roomListResponse");
      }
      else {
         jsonObj.put("action", "roomListResponse");
      }
      return jsonObj.toString();
   }

   @Override
   public String getPacketName() {
      return "roomListResponse";
   }

   @Override
   public int describeContents() {
      return 0;
   }

   @Override
   public void writeToParcel(Parcel arg0, int arg1) {

   }
}
